package com.guigu.financial.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.guigu.financial.enums.IntegralEnum;
import com.guigu.financial.mapper.BorrowerMapper;
import com.guigu.financial.mapper.UserInfoMapper;
import com.guigu.financial.mapper.UserIntegralMapper;
import com.guigu.financial.pojo.Borrower;
import com.guigu.financial.pojo.UserInfo;
import com.guigu.financial.pojo.UserIntegral;
import com.guigu.financial.pojo.vo.BorrowerApprovalVO;
import com.guigu.financial.service.BorrowerService;
import com.guigu.financial.service.DictService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class BorrowerServiceImpl implements BorrowerService {
    @Autowired
    private BorrowerMapper borrowerMapper;

    @Autowired
    private UserInfoMapper userInfoMapper;

    @Autowired
    private UserIntegralMapper userIntegralMapper;

    @Autowired
    private DictService dictService;

    @Override
    public int deleteByPrimaryKey(Long id) {
        return borrowerMapper.deleteByPrimaryKey(id);
    }

    @Override
    public int insert(Borrower record) {
        return borrowerMapper.insert(record);
    }

    @Override
    public int insertSelective(Borrower record) {
        return borrowerMapper.insertSelective(record);
    }

    @Override
    public Borrower selectByPrimaryKey(Long id) {
        return borrowerMapper.selectByPrimaryKey(id);
    }

    @Override
    public int updateByPrimaryKeySelective(Borrower record) {
        return borrowerMapper.updateByPrimaryKeySelective(record);
    }

    @Override
    @Transactional
    public void approval(BorrowerApprovalVO borrowerApprovalVO) {
        Long borrowerId = borrowerApprovalVO.getBorrowerId();
        Borrower borrower = borrowerMapper.selectByPrimaryKey(borrowerId);
        Integer status = borrowerApprovalVO.getStatus();
        //设置审核状态
        borrower.setStatus(status);
        borrowerMapper.updateByPrimaryKeySelective(borrower);

        Long userId = borrower.getUserId();
        UserInfo userInfo = userInfoMapper.selectByPrimaryKey(userId);
        // 获取会员原始积分
        Integer integral = userInfo.getIntegral();

        UserIntegral userIntegral = new UserIntegral();
        userIntegral.setUserId(userId);
        userIntegral.setIntegral(borrowerApprovalVO.getIntegral());
        userIntegral.setContent("借款人基本信息");
        userIntegralMapper.insertSelective(userIntegral);
        int currentIntegral = integral + borrowerApprovalVO.getIntegral();

        //设置附件积分（身份证）
        if (borrowerApprovalVO.getX()){
            userIntegral = new UserIntegral();
            userIntegral.setUserId(userId);
            userIntegral.setIntegral(IntegralEnum.BORROWER_IDCARD.getIntegral());
            userIntegral.setContent(IntegralEnum.BORROWER_IDCARD.getMsg());
            userIntegralMapper.insertSelective(userIntegral);
            currentIntegral += IntegralEnum.BORROWER_IDCARD.getIntegral();
        }

        //设置附件积分（房产）
        if (borrowerApprovalVO.getY()){
            userIntegral = new UserIntegral();
            userIntegral.setUserId(userId);
            userIntegral.setIntegral(IntegralEnum.BORROWER_HOUSE.getIntegral());
            userIntegral.setContent(IntegralEnum.BORROWER_HOUSE.getMsg());
            userIntegralMapper.insertSelective(userIntegral);
            currentIntegral += IntegralEnum.BORROWER_HOUSE.getIntegral();
        }
        //设置附件积分（车辆）
        if (borrowerApprovalVO.getZ()){
            userIntegral = new UserIntegral();
            userIntegral.setUserId(userId);
            userIntegral.setIntegral(IntegralEnum.BORROWER_CAR.getIntegral());
            userIntegral.setContent(IntegralEnum.BORROWER_CAR.getMsg());
            userIntegralMapper.insertSelective(userIntegral);
            currentIntegral += IntegralEnum.BORROWER_CAR.getIntegral();
        }

        //设置总积分
        userInfo.setIntegral(currentIntegral);
        //修改审核状态
        userInfo.setBorrowAuthStatus(borrowerApprovalVO.getStatus());
        userInfoMapper.updateByPrimaryKeySelective(userInfo);


    }

    @Override
    public PageInfo findByBorrower(int pageNum, int pageSize, Borrower borrower) {
        PageHelper.startPage(pageNum,pageSize);
        List<Borrower> borrowers = borrowerMapper.findByBorrower(borrower);
        PageInfo pageInfo = new PageInfo(borrowers);
        return pageInfo;
    }

    @Override
    public PageInfo findAll(int pageNum, int pageSize) {
        PageHelper.startPage(pageNum,pageSize);
        List<Borrower> borrowers = borrowerMapper.findAll();

        return new PageInfo(borrowers);
    }

    @Override
    public int updateByPrimaryKey(Borrower record) {
        return borrowerMapper.updateByPrimaryKey(record);
    }
}
